package toponet.geometry;

public class Triangle {
	Point [] vertices;
	
	public Triangle(double x1, double y1, double x2, double y2, double x3, double y3) {
		this.vertices = new Point[3];
		this.vertices[0] = new Point(x1,y1);
		this.vertices[1] = new Point(x2,y2);
		this.vertices[2] = new Point(x3,y3);
	}
	
	public Triangle(Point a, Point b, Point c) { 
		//maybe stupid, use copy constructors instead? java noob :P
		this.vertices = new Point[3]; 
		this.vertices[0] = new Point(a.getX(), a.getY());
		this.vertices[1] = new Point(b.getX(), b.getY());
		this.vertices[2] = new Point(c.getX(), c.getY());
	}
	
	public boolean containsPoint(double x,double y) {
		int crossings = 0;
        for (int i = 0; i < 2; i++) {
            double slope = (this.vertices[i+1].getX() - this.vertices[i].getX())  / (this.vertices[i+1].getY() - this.vertices[i].getY());
            boolean cond1 = (this.vertices[i].getY() <= y) && (y < this.vertices[i+1].getY());
            boolean cond2 = (this.vertices[i+1].getY() <= y) && (y < this.vertices[i].getY());
            boolean cond3 = x <  slope * (y - this.vertices[i].getY()) + this.vertices[i].getX();
            if ((cond1 || cond2) && cond3) crossings++;
        }
        return (crossings % 2 != 0);
	}
	
	public Point getPoint(boolean b) { 
		return b == true ? vertices[1] : vertices[2];
	}
	
	public Point[] getPoint() {
		return this.vertices;
	}
	
	public boolean containsPoint(Point p) {
		return this.containsPoint(p.getX(),p.getY());
	}
}
